import { Plugin, WorkspaceLeaf } from 'obsidian';
import { CodeOutlineView, CODE_OUTLINE_VIEW } from './view';

export default class CodeOutlinePlugin extends Plugin {
    async onload() {
        // 注册自定义视图
        this.registerView(
            CODE_OUTLINE_VIEW,
            (leaf) => new CodeOutlineView(leaf)
        );

        // 添加功能区按钮
        this.addRibbonIcon('code', '代码大纲视图', () => {
            this.activateView();
        });
    }

    async onunload() {
        // 清理视图
        this.app.workspace.detachLeavesOfType(CODE_OUTLINE_VIEW);
    }

    async activateView() {
        const { workspace } = this.app;
        
        // 如果视图已经打开，就激活它
        let leaf = workspace.getLeavesOfType(CODE_OUTLINE_VIEW)[0];
        
        if (!leaf) {
            // 创建新的视图
            leaf = workspace.getRightLeaf(false);
            await leaf.setViewState({
                type: CODE_OUTLINE_VIEW,
                active: true,
            });
        }
        
        // 聚焦到视图
        workspace.revealLeaf(leaf);
    }
} 